﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication
{
    class GraphPlotter
    {
        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.ComboBox comboBoxXaxis;
        private System.Windows.Forms.ComboBox comboBoxYaxis;



        public GraphPlotter(System.Windows.Forms.DataVisualization.Charting.Chart chart1, System.Windows.Forms.DataGridView dataGridView1, System.Windows.Forms.ComboBox comboBoxXaxis, System.Windows.Forms.ComboBox comboBoxYaxis)
        {
            // TODO: Complete member initialization
            this.chart1 = chart1;
            this.dataGridView1 = dataGridView1;
            this.comboBoxXaxis = comboBoxXaxis;
            this.comboBoxYaxis = comboBoxYaxis;

        }

        public void graphPlot()
        {

            if (comboBoxXaxis.SelectedIndex < 0 || comboBoxYaxis.SelectedIndex < 0)
            {
                return;
            }
            if (chart1.ChartAreas.Count > 0)
            {
                if (chart1.ChartAreas[0].Name == "area")
                {
                    chart1.ChartAreas.RemoveAt(0);
                }
            }
            if (chart1.Series.Count > 0)
            {
                if (chart1.Series[0].Name == "graph")
                {
                    chart1.Series.RemoveAt(0);
                }
            }

            chart1.Series.Add("graph");
            chart1.ChartAreas.Add("area");
            chart1.Series["graph"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1[comboBoxXaxis.SelectedIndex, i].Value == null && dataGridView1[comboBoxYaxis.SelectedIndex, i].Value == null)
                {
                    chart1.Series["graph"].Points.AddXY(0, 0);
                    continue;
                }
                if (dataGridView1[comboBoxXaxis.SelectedIndex, i].Value == null)
                {
                    chart1.Series["graph"].Points.AddXY(0, dataGridView1[comboBoxYaxis.SelectedIndex, i].Value);
                    continue;
                }
                if (dataGridView1[comboBoxYaxis.SelectedIndex, i].Value == null)
                {
                    chart1.Series["graph"].Points.AddXY(dataGridView1[comboBoxXaxis.SelectedIndex, i].Value, 0);
                    continue;
                }
                chart1.Series["graph"].Points.AddXY(dataGridView1[comboBoxXaxis.SelectedIndex, i].Value, dataGridView1[comboBoxYaxis.SelectedIndex, i].Value);
            }


        }
        private double maximumOf(int columnIndex)
        {
            double maxinumValue = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToDouble(dataGridView1[columnIndex, i].Value.ToString()) > maxinumValue)
                {
                    maxinumValue = Convert.ToDouble(dataGridView1[columnIndex, i].Value.ToString());
                }
            }
            return maxinumValue;
        }
    }
}
